package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.ActivityDto;
import com.qk.entity.Activity;
import com.qk.mapper.ActivityMapper;
import com.qk.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public void deleteActivityById(Integer id) {
        activityMapper.deleteById(id);
    }

    @Override
    public void updateActivity(Activity activity) {
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.updateActivity(activity);
    }

    @Override
    public void addActivity(Activity activity) {
        // 将前端传入的日期字符串转换为LocalDateTime
        activity.setStartTime(activity.getStartTime());//LocalDateTime.parse(activity.getStartTimeStr(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        activity.setEndTime(activity.getEndTime());//LocalDateTime.parse(activity.getEndTimeStr(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.insertActivity(activity);
    }


    @Override
    public Activity findActivityById(Integer id) {
        return activityMapper.selectById(id);
    }

    @Override
    public PageResult<Activity> findActivity(ActivityDto activityDto) {
        // 开始分页
        PageHelper.startPage(activityDto.getPage(), activityDto.getPageSize());

        // 执行查询
        List<Activity> activities = activityMapper.select(activityDto);

        // 获取分页信息
        Page<Activity> pageInfo = (Page<Activity>) activities;

        // 封装成 PageResult 对象
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getResult());
    }
}
